package linearSearch;


public class LinearSearch {
    private LinearSearch() {
    } //将构造器设为私有。无法创建类实例。

    public static <E> int search(E[] data, E target) {
        for (int i = 0; i < data.length; i++)
            if (data[i].equals(target))
                return i;

        return -1;
    }

    //    public static void main(String[] args) {
    //将search方法修改为static的更合理。正如Math.abs()。
//        int[] data = {24, 18, 12, 9, 16, 66, 32, 4};
//        LinearSearch ls = new LinearSearch();
//        int res = ls.search(data, 16);
//        System.out.println(res);
//
//        int res2 = ls.search(data, 666);
//        System.out.println(res2);
//    }
    public static void main(String[] args) {
        int[] dataSize = {1000000, 10000000};
        int k = 100;
        for (int n : dataSize) {
            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            long startTime = System.nanoTime();
            for (int i = 0; i < k; i++) {
                LinearSearch.search(data, n);
            }
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1e9;
            System.out.println("n = " + n + ", " + k + " runs, " + time + " s.");

        }
    }
}
